﻿using System;
using System.Collections.Generic;
using System.Linq;
using StockCrawlerLib.DAL;
using StockCrawlerLib;
using CrawlerController;

namespace StockCrawler
{
    public partial class UpdateHistory : System.Web.UI.Page
    {
        private HistoryRepository _historyRepository = new HistoryRepository();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!this.IsPostBack)
            {
                dateStart.Date = DateTime.Now;
                dateEnd.Date = DateTime.Now;
                gridHistory.DataBind();
            }
        }

        private void FptsImport()
        {
            BaseCrawler objCrawler = new FptsCrawler();
            DateTime dt = dateStart.Date;
            while (dt <= dateEnd.Date)
            {
                if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
                    continue;

                //Lấy dữ liệu price
                objCrawler.ImportPriceHisotry("", dt);

                //Lấy dữ liệu đặt lệnh
                objCrawler.ImportOrderHisotry("", dt);

                //Lấy dữ liệu giao dịch nước ngoài
                objCrawler.ImportForeignHistory(dt);

                dt = dt.AddDays(1);

                //Lấy dữ liệu giao dịch nước ngoài
                objCrawler.ImportForeign2History(dateStart.Date);
            }
            gridHistory.DataBind();
            //gridViewLog.ItemsSource = SystemConfig.SystemLog;
        }
        protected void btnUpdateHistory_Click(object sender, EventArgs e)
        {
            FptsImport();
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            gridHistory.DataBind();
        }

        protected void LinqServerModeDataSourceHistory_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
        {
            e.KeyExpression = "id_stock";
            e.QueryableSource = _historyRepository.SelectByNgayGiaoDich(dateStart.Date);
        }
    }
}